package com.boc.ewr.business.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boc.ewr.business.entity.SystemProcessQuestion;
import com.boc.ewr.business.service.ISystemProcessQuestionService;
import com.boc.ewr.util.MsgCategoryEnum;
import com.boc.ewr.util.ToUserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.dto.message.MessageDTO;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.service.ISysDepartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * @Description: 制度及流程-提问
 * @Author: jeecg-boot
 * @Date: 2023-06-18
 * @Version: V1.0
 */
@Api(tags = "制度及流程-提问")
@RestController
@RequestMapping("/business/systemProcessQuestion")
@Slf4j
public class SystemProcessQuestionController extends JeecgController<SystemProcessQuestion, ISystemProcessQuestionService> {
    @Autowired
    private ISystemProcessQuestionService systemProcessQuestionService;
    @Autowired
    private ISysBaseAPI sysBaseAPI;

    @Autowired
    private ISysDepartService sysDepartService;
    /**
     * 分页列表查询
     *
     * @param systemProcessQuestion
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "制度及流程-提问-分页列表查询")
    @ApiOperation(value = "制度及流程-提问-分页列表查询", notes = "制度及流程-提问-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<SystemProcessQuestion>> queryPageList(SystemProcessQuestion systemProcessQuestion,
                                                              @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                              HttpServletRequest req) {
        QueryWrapper<SystemProcessQuestion> queryWrapper = QueryGenerator.initQueryWrapper(systemProcessQuestion, req.getParameterMap());
        Set<String> orgCodeSetByLoginUser = sysBaseAPI.getOrgCodeSetByLoginUser(this.getLoginUser());
        if (orgCodeSetByLoginUser!=null) {
            queryWrapper.in("org_code", orgCodeSetByLoginUser);
        }
        Page<SystemProcessQuestion> page = new Page<SystemProcessQuestion>(pageNo, pageSize);
        IPage<SystemProcessQuestion> pageList = systemProcessQuestionService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 回显接口
     *
     * @return
     */
    //@AutoLog(value = "制度及流程-提问-分页列表查询")
    @ApiOperation(value = "制度及流程-提问-分页列表查询", notes = "制度及流程-提问-分页列表查询")
    @GetMapping(value = "/getInfo")
    public Result<SystemProcessQuestion> getInfo() {
        LoginUser loginUser = this.getLoginUser();
        SystemProcessQuestion systemProcessQuestion = new SystemProcessQuestion();
        systemProcessQuestion.setOrgCode(loginUser.getOrgCode());
        systemProcessQuestion.setEmpName(loginUser.getRealname());
        return Result.OK(systemProcessQuestion);
    }

    /**
     * 添加
     *
     * @param systemProcessQuestion
     * @return
     */
    @AutoLog(value = "制度及流程-提问-添加")
    @ApiOperation(value = "制度及流程-提问-添加", notes = "制度及流程-提问-添加")
    //@RequiresPermissions("com.boc.ewr:system_process_question:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody SystemProcessQuestion systemProcessQuestion) {
        systemProcessQuestion.setCreateTime(new Date());
        systemProcessQuestionService.save(systemProcessQuestion);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param systemProcessQuestion
     * @return
     */
    @AutoLog(value = "制度及流程-提问-编辑")
    @ApiOperation(value = "制度及流程-提问-编辑", notes = "制度及流程-提问-编辑")
    //@RequiresPermissions("com.boc.ewr:system_process_question:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody SystemProcessQuestion systemProcessQuestion) {
        systemProcessQuestionService.updateById(systemProcessQuestion);
        return Result.OK("编辑成功!");
    }

    /**
     * 回复
     *
     * @param systemProcessQuestion
     * @return
     */
    @AutoLog(value = "制度及流程-提问-回复")
    @ApiOperation(value = "制度及流程-提问-回复", notes = "制度及流程-提问-回复")
    //@RequiresPermissions("com.boc.ewr:system_process_question:answer")
    @RequestMapping(value = "/answer", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> answer(@RequestBody SystemProcessQuestion systemProcessQuestion) {
        SystemProcessQuestion byId = systemProcessQuestionService.getById(systemProcessQuestion.getId());

        byId.setAnswerPerson(this.getLoginUser().getRealname());
        byId.setAnswerTime(new Date());
        byId.setAnswerStatus("1");

        systemProcessQuestionService.updateById(byId);
        return Result.OK("回复成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "制度及流程-提问-通过id删除")
    @ApiOperation(value = "制度及流程-提问-通过id删除", notes = "制度及流程-提问-通过id删除")
    //@RequiresPermissions("com.boc.ewr:system_process_question:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        systemProcessQuestionService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "制度及流程-提问-批量删除")
    @ApiOperation(value = "制度及流程-提问-批量删除", notes = "制度及流程-提问-批量删除")
    //@RequiresPermissions("com.boc.ewr:system_process_question:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.systemProcessQuestionService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "制度及流程-提问-通过id查询")
    @ApiOperation(value = "制度及流程-提问-通过id查询", notes = "制度及流程-提问-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<SystemProcessQuestion> queryById(@RequestParam(name = "id", required = true) String id) {
        SystemProcessQuestion systemProcessQuestion = systemProcessQuestionService.getById(id);
        if (systemProcessQuestion == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(systemProcessQuestion);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param systemProcessQuestion
     */
    //@RequiresPermissions("com.boc.ewr:system_process_question:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SystemProcessQuestion systemProcessQuestion) {
        return super.exportXls(request, systemProcessQuestion, SystemProcessQuestion.class, "制度及流程-提问");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    //@RequiresPermissions("system_process_question:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SystemProcessQuestion.class);
    }

    // 获取登录角色
    private LoginUser getLoginUser() {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        return sysUser;
    }

    private void sendMsg(SystemProcessQuestion systemProcessQuestion) {
        Set<String> orgSet = new HashSet();
        //通过当前机构获取上级机构
        String orgCode =systemProcessQuestion.getOrgCode();
        if(StrUtil.isNotBlank(orgCode)){
            SysDepart sysDepart = sysDepartService.queryCompByOrgCode(orgCode);
            String parentId =sysDepart.getParentId();
            if(StrUtil.isNotBlank(parentId)){
                //结构号和机构表的id一致，所有可直接用结构化查询
                SysDepart parentSysDepart = sysDepartService.queryCompByOrgCode(parentId);
                if(parentSysDepart !=null ){
                    orgSet.add(parentSysDepart.getOrgCode());
                }
            }else{
                orgSet.add(orgCode);
            }
        }
        // 信息推送至网点对账人员
        String toUser = ToUserUtil.genToUser(orgSet);

        if (toUser != null) {
            MessageDTO messageDTO = new MessageDTO(
                    systemProcessQuestion.getEmpName(),
                    toUser,
                    systemProcessQuestion.getQuesTitle(),
                    systemProcessQuestion.getQuesContent(),
                    MsgCategoryEnum.PROCESS_QUESTION.getIndex());
            sysBaseAPI.sendSysAnnouncement(messageDTO);
        }

    }

}
